package com.xjl.controller;

import com.xjl.domain.ResponseResult;
import com.xjl.domain.SystemUser;
import com.xjl.service.SystemUserService;
import com.xjl.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sys_user")
public class SystemUserController {
    @Autowired
    private SystemUserService systemUserService;

    @PostMapping("/login")
    public ResponseResult login(@RequestBody SystemUser systemUser) {
        //校验用户密码是否正确
        SystemUser loginUser = systemUserService.login(systemUser);
        //如果正确，生成token返回
        Map<String, Object> map;
        if (loginUser != null) {
            map = new HashMap<>();

            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), String.valueOf(loginUser.getId()), null);
            map.put("token", token);
        } else {
            //如果不正确，给出提示
            return new ResponseResult<>(300, "用户名或密码错误，重新登陆");
        }

        return new ResponseResult<>(200, "登陆成功", map);
    }
}
